/**
 *	@project:	kybernetes
 *	@author:	D.Toepel
 *	@note:		---
 *	
 *	Copyright (C) 2009  D.Toepel <de.dtools> {at} <gmail> {dot} <com>
 *	
 *	This program is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *	
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *	
 *	You should have received a copy of the GNU General Public License
 *	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 */
package dt.htwk.extras;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.sql.Timestamp;
import java.util.Date;

import javax.swing.JTextArea;
import javax.swing.SwingUtilities;

/**
 * <h3>Template</h3>
 *
 * @author DToepel
 *
 */
public class TestServerThread extends Thread {
	
/* ****************************************************
 * Deklaration der Variablen und Komponenten
 * ***************************************************/

	/**
	 * serialVersionUID -- Standard: 1L
	 */
	private static final long serialVersionUID = 1L;

	private static Integer port;
	
	private Thread t;

	private Timestamp time;

	private ServerSocket server;

	private Socket client;

	private String strInput;

	private static JTextArea output;
	

/* ****************************************************
 * Aufruf des Konstruktors
 * ***************************************************/

	public TestServerThread(){
		super();
	}
	
/* ****************************************************
 * PUBLIC - oeffentliche Methoden
 * ***************************************************/

	/* (non-Javadoc)
	 * @see java.lang.Thread#start()
	 */
	@Override
	public void start(){
		if(t==null){
			t = new Thread(this);
			t.start();
		}
	}
	
	/**
	 * <h3>stopThread</h3>
	 *
	 */
	public void stopThread(){
		try {
			server.close();
		} catch (IOException e) {}
		t = null;
	}
	
	/**
	 * <h3>setOutput</h3>
	 *
	 * @param output
	 */
	public void setOutput(JTextArea output){
		TestServerThread.output = output;
	}
	
	/**
	 * <h3>setPort</h3>
	 *
	 * @param port
	 */
	public void setPort(Integer port) {
		TestServerThread.port = port;
	}
	
	synchronized public void run(){

		time = new Timestamp(new Date().getTime());
		clearTextMessage();
		setTextMessage("Start Server - " + time);
		
		try {
			server = new ServerSocket(port);
		} catch (Exception e) {}
		
		Thread me = Thread.currentThread();
		while(me == t)  {
			
			StringBuilder sb = new StringBuilder(255);
			try {
				client= server.accept();

				InputStream is = client.getInputStream();
				InputStreamReader isr = new InputStreamReader(is); 
				BufferedReader in = new BufferedReader(isr); 

				OutputStream os = client.getOutputStream();
				OutputStreamWriter osw = new OutputStreamWriter(os); 
				BufferedWriter out = new BufferedWriter(osw);
				
				try {
					while ((strInput = in.readLine()) != null) {
						if (strInput.length() > 0) {
							sb.append(strInput);
						}
					}
				} catch (Exception e) {}

				strInput = sb.toString();

				out.write(strInput + "\0");
				out.flush();				
				
				out.close();
				in.close();
				client.close();
				
				// Werte mit DisplayManager schreiben
				setTextMessage();
				
			} catch (Exception e) {}
			try {
				sleep(0);
			} catch (InterruptedException e) {}
		}		
		time = new Timestamp(new Date().getTime());
		setTextMessage("Stop Server - " + time);
	}
	

/* ****************************************************
 * PRIVATE - nicht-oeffentliche Methoden
 * ***************************************************/

	private void setTextMessage(){
		Runnable r = new Runnable(){
			@Override
			public void run() {
				output.append("\n" + strInput);
			}	
		};
		SwingUtilities.invokeLater(r);				
	}

	private void setTextMessage(String str){
		Runnable r = new Runnable(){
			@Override
			public void run() {
				output.append("\n" + strInput);
			}	
		};
		strInput = str;
		SwingUtilities.invokeLater(r);				
	}

	private void clearTextMessage(){
		Runnable r = new Runnable(){
			@Override
			public void run() {
				output.setText("");
			}	
		};
		SwingUtilities.invokeLater(r);				
	}

}
